"""
    Software : PyCharm
    File     : serializers.py
    Time     : 2021/5/31 17:27
    Author   : Pontoon
    describe :
"""
from rest_framework import serializers

from .models import Message


class MessageSerializer(serializers.ModelSerializer):
    from_user = serializers.SerializerMethodField()
    to_user = serializers.SerializerMethodField()

    class Meta:
        model = Message
        fields = ["from_user", "to_user", "content", "create_time"]

    def get_from_user(self, instance):
        data = {}
        data['uid'] = instance.from_user.uid
        data['nickname'] = instance.from_user.nickname
        data['avatar'] = instance.from_user.avatar
        return data

    def get_to_user(self, instance):
        data = {}
        data['uid'] = instance.to_user.uid
        data['nickname'] = instance.to_user.nickname
        data['avatar'] = instance.to_user.avatar
        return data

